/* channel相关的vuex操作 */
import { getUserChannel, getAllChannel } from '@/api/channel'
export default {
  state: {
    channelList: [],
    AllchannelList: []
  },
  mutations: {
    // 获取用户channel列表
    setUserChannel(state, payload) {
      state.channelList = payload
    },
    // 获取所有channel列表
    setAllUserChannel(state, payload) {
      state.AllchannelList = payload
    },
    // 增加
    addUserChannel(state, payload) {
      state.channelList.push(payload)
    },
    // 删除
    delUserChannel(state, payload) {
      state.channelList = state.channelList.filter(item => item.id !== payload.id)
    }
  },
  actions: {
    async setUserChannelAsync(context) {
      const { data } = await getUserChannel()
      context.commit('setUserChannel', data.channels)
    },
    async setAllUserChannelAsync(context) {
      const { data } = await getAllChannel()
      console.log(data.channels)
      context.commit('setAllUserChannel', data.channels)
    }
  },
  getters: {
    optionsChannelList(state) {
      return state.AllchannelList.filter(item => {
        return !state.channelList.some(obj => obj.id === item.id)
      })
    }
  },
  namespaced: true
}
